package top.yokey.shopnc.activity.seller;

import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.RelativeLayout;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import top.yokey.base.base.BaseCountTime;
import top.yokey.base.base.BaseImageLoader;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.base.base.BaseToast;
import top.yokey.base.util.JsonUtil;
import top.yokey.shopnc.R;
import top.yokey.shopnc.base.BaseApplication;
import top.yokey.shopnc.base.BaseConstant;
import top.yokey.base.base.BaseHttpClient;
import top.yokey.base.base.BaseHttpListener;
import top.yokey.base.base.BaseShared;
import top.yokey.shopnc.base.SwipeActivity;
import top.yokey.base.bean.BaseBean;
import top.yokey.shopnc.bean.SellerIndexBean;
import top.yokey.shopnc.model.SellerIndexModel;

@ContentView(R.layout.activity_seller_seller)
public class SellerActivity extends SwipeActivity {

    @ViewInject(R.id.mainScrollView)
    private NestedScrollView mainScrollView;

    @ViewInject(R.id.avatarImageView)
    private AppCompatImageView avatarImageView;
    @ViewInject(R.id.nicknameTextView)
    private AppCompatTextView nicknameTextView;
    @ViewInject(R.id.saleDayTextView)
    private AppCompatTextView saleDayTextView;
    @ViewInject(R.id.saleMonthTextView)
    private AppCompatTextView saleMonthTextView;
    @ViewInject(R.id.saleIngTextView)
    private AppCompatTextView saleIngTextView;

    @ViewInject(R.id.orderRelativeLayout)
    private RelativeLayout orderRelativeLayout;

    private SellerIndexBean sellerIndexBean;

    @Override
    public void initData() {

        BaseHttpClient.get().updateKey(BaseShared.get().getString(BaseConstant.SHARED_SELLER_KEY));

        getData();

    }

    @Override
    public void initEven() {

        orderRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startOrderSeller(getActivity(), 0);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseHttpClient.get().updateKey("");
    }

    //自定义方法

    private void getData() {

        SellerIndexModel.get().index(new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                sellerIndexBean = JsonUtil.json2Bean(baseBean.getDatas(), SellerIndexBean.class);
                BaseImageLoader.get().displayCircle(sellerIndexBean.getStoreInfo().getStoreAvatar(), avatarImageView);
                String temp = sellerIndexBean.getSellerInfo().getSellerName() + "（" + sellerIndexBean.getStoreInfo().getGradeName() + "）";
                nicknameTextView.setText(temp);
                temp = sellerIndexBean.getStoreInfo().getDailySales().getOrdernum() + "\n昨日销量";
                saleDayTextView.setText(temp);
                temp = sellerIndexBean.getStoreInfo().getMonthlySales().getOrdernum() + "\n当月销量";
                saleMonthTextView.setText(temp);
                temp = sellerIndexBean.getStatics().getOnline() + "\n出售中";
                saleIngTextView.setText(temp);
            }

            @Override
            public void onFailure(String reason) {
                if (reason.equals("请登录")) {
                    BaseToast.get().show("身份信息已失效，请重新登录！");
                    BaseHttpClient.get().updateKey("");
                    BaseShared.get().putString(BaseConstant.SHARED_SELLER_KEY, "");
                    BaseApplication.get().start(getActivity(), LoginActivity.class);
                    BaseApplication.get().finish(getActivity());
                    return;
                }
                BaseSnackBar.get().show(mainScrollView, reason);
                new BaseCountTime(BaseConstant.TIME_COUNT, BaseConstant.TIME_TICK) {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        getData();
                    }
                }.start();
            }
        });

    }

}
